package com.itboye.sunsun.person.ui;

import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.LocationBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DetailLocationActivity extends BaseActivity implements OnClickListener {

	private RelativeLayout detailrela;
	private RelativeLayout namerela;
	private RelativeLayout phonerela;
	private RelativeLayout ziprela;
	private RelativeLayout backrela;
	private RelativeLayout deleterela;
	private RelativeLayout saverela;
	private RelativeLayout defaultrela;

	private RelativeLayout defaultrela1;

	private TextView provincetext;
	private EditText detailtext;
	private EditText nametext;

	boolean status = false;

	private ImageView defaultbutton;

	private EditText phonetext;
	private EditText ziptext;
	private String id;
	private String defaultid;
	

	

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_detaillocation);
		setStatusBarColor(R.color.top_blue);
		initview();
		getDefaultLocation();

	}

	@SuppressLint("NewApi")
	public void initview() {
		Intent intent = getIntent();
		String wholelocation = intent.getStringExtra("wholelocation");
		String detail = intent.getStringExtra("detail");
		String name = intent.getStringExtra("name");
		String phone = intent.getStringExtra("phone");
		String zipcode = intent.getStringExtra("zipcode");

		id = intent.getStringExtra("id");
		provincetext.setText(wholelocation);
		detailtext.setText(detail);
		nametext.setText(name);
		phonetext.setText(phone);
		ziptext.setText(zipcode);

		// Toast.makeText(App.ctx, id, 0).show();

	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.detailrela:

			break;
		case R.id.backrela:
			this.finish();
			break;
		case R.id.saverela:
			updateLocation();
			if (status) {
				setdefaultLocation();
			}
			Intent intent=new Intent(AddLocationActivity.CHANGEADDRESS);
			sendBroadcast(intent);
			break;
		case R.id.deleterela:

			// 默认地址不可以删除
			if (defaultid.equals(id)) {
				Toast.makeText(App.ctx, "默认地址不可以删除", 0).show();
			} else {
				deleteLocation();
			}

			break;
		case R.id.defaultbutton:

			if (!status) {
				defaultbutton.setImageDrawable(getResources().getDrawable(R.drawable.default_on));
				status = !status;
				// Toast.makeText(App.ctx, ""+status,0).show();
				// setdefaultLocation();
			} else {
				defaultbutton.setImageDrawable(getResources().getDrawable(R.drawable.default_off));
				status = !status;
			}
			break;

		default:
			break;
		}

	}

	// 更新地址
	public void updateLocation() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100").typeKey("BY_Address_update")
				.param("id", id).param("detailinfo", "" + detailtext.getText().toString())
				.param("mobile", "" + phonetext.getText().toString())
				.param("postal_code", "" + ziptext.getText().toString())
				.param("contactname", "" + nametext.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DetailLocationActivity.this.finish();

						Intent intent = new Intent("com.itboye.changeaddress");
						sendBroadcast(intent);

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						// Toast.makeText(App.ctx, code + "", 0).show();
						if (exception instanceof CodeErrorException) {
							// Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, "服务器出错了", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	// 删除地址
	public void deleteLocation() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100").typeKey("BY_Address_delete")
				.param("id", id).requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						DetailLocationActivity.this.finish();

						Intent intent = new Intent("com.itboye.changeaddress");
						sendBroadcast(intent);

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						// Toast.makeText(App.ctx, code + "", 0).show();
						if (exception instanceof CodeErrorException) {
							// Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, "服务器出错了", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	// 设置默认地址
	public void setdefaultLocation() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100")
				.typeKey("BY_Address_setDefaultAddress").param("id", id)
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {

						//
						// Intent intent=new Intent("com.itboye.changeaddress");
						// sendBroadcast(intent);

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						// Toast.makeText(App.ctx, code + "", 0).show();
						if (exception instanceof CodeErrorException) {
							// Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, "服务器出错了", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}

	// 获取默认地址ID
	public void getDefaultLocation() {

		// Toast.makeText(App.ctx, (String)SPUtils.get(App.ctx, null,
		// SPContants.USER_ID, ""), 0).show();
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100")
				.typeKey("BY_Address_getDefaultAddress")
				.param("uid", (String) SPUtils.get(App.ctx, null, SPContants.USER_ID, ""))

				.requestListener(new XRequestListener<String>() {

					@SuppressLint("NewApi")
					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());

						if (!response.toString().equals("0")) {
							defaultid = response.toString();

							// Toast.makeText(App.ctx, defaultid.toString(),
							// 0).show();
							if (defaultid.equals(id)) {
								defaultbutton.setImageDrawable(getResources().getDrawable(R.drawable.default_on));
								status = !status;
							}

							// Toast.makeText(App.ctx, response.toString(),
							// 0).show();
						} else {
							defaultid="";
//							Toast.makeText(App.ctx, response.toString(), 0).show();
						}
						// getSingleLocation(response.toString());
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						Toast.makeText(App.ctx, code + "", 0).show();
						if (exception instanceof CodeErrorException) {
							// Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, "服务器出错了", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
